<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Ver 3.0, Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/
class ClsLStatement extends ClsLTokenizeBase
{
    
    public function __construct($arrToken,$isSingleStatment=false)
    {
        parent::__construct($arrToken,$isSingleStatment);
    }
    public function getStatementType()
    {
        return $this->statementType;
    }
    public function getStatementName()
    {
        return $this->statementName;
    }
    public function getAssignmentStatementData($param)
    {
        if($this->statementType===T_VARIABLE)
        {
            if($this->arrToken[$this->arrValidTokenPos[2]][0]===T_ARRAY)
            {
                /**
                 * the 3rd valid token will point the "(". so we are continuing from 4
                 */
                for($i=4;$i<count($this->arrValidTokenPos);$i++)
                {
                    if(is_array($this->arrToken[$this->arrValidTokenPos[$i]]) && $this->arrToken[$this->arrValidTokenPos[$i]][1]==$param)
                    {
                        return $this->arrToken[$this->arrValidTokenPos[$i+2]][1];
                    }
                }
            }
            else
            {
                return $this->arrToken[$this->arrValidTokenPos[2]][1];
            }
        }
        return false;
    }
    public function setAssignmentStatementData($data,$param=false)
    {
        if($this->statementType===T_VARIABLE)
        {
            if($this->arrToken[$this->arrValidTokenPos[2]][0]===T_ARRAY)
            {
                /**
                 * the 3rd valid token will point the "(". so we are continuing from 4
                 */
                for($i=4;$i<count($this->arrValidTokenPos);$i++)
                {
                    if(is_array($this->arrToken[$this->arrValidTokenPos[$i]]) && $this->arrToken[$this->arrValidTokenPos[$i]][1]==$param)
                    {
                        $this->arrToken[$this->arrValidTokenPos[$i+2]][1]=$data;
                        return true;
                    }
                }
            }
            else
            {
                $this->arrToken[$this->arrValidTokenPos[2]][1]=$value;
                return true;
            }
        }
        return false;
    }
    public function getArrayParam($num)
    {
        if(isset($this->arrayParam[$num])) return $this->arrayParam[$num];
        return false;
    }
    public function &getAssignmentStatement($name,$arrParam=array())
    {
        $arrObjStatement=array();
        foreach($this->arrStatementObject as $index=>$objStatement)
        {
            if($objStatement->getStatementType() === T_VARIABLE)
            {
                if($objStatement->getStatementName()==$name)
                {
                    if($arrParam)
                    {
                        $isMatch=true;
                        foreach($arrParam as $ind=>$param)
                        {
                           if($objStatement->getArrayParam($ind)!=$param) 
                           {
                               $isMatch=false;
                               break;
                           }
                        }
                        if($isMatch)
                        {
                            $arrObjStatement[]=$this->arrStatementObject[$index];
                        }
                    }
                    else
                        $arrObjStatement[]=$this->arrStatementObject[$index];
                }
            }
        }
        if($arrObjStatement) return $arrObjStatement;
        $ret=false;
        return $ret;
    }
    
    public function &getStatementByPosition($pos,$type=false)
    {
        if($type===false) return $this->arrStatementObject[$pos];
        $count=0;
        foreach($this->arrStatementObject as $index=>$objStatement)
        {
            if($objStatement->getStatementType() === $type)
            {
                if($count===$pos)
                {
                    return $this->arrStatementObject[$index];
                }
                $count=$count+1;
            }
        }
        return false;
    }
    public function setAssignmentStatementValue($value)
    {
        if($this->statementType===T_VARIABLE)
        {
            $equalTokenPos=$this->getNextToken(0);
            $valueTokenPos=$this->getNextToken($equalTokenPos);
            $this->arrToken[$valueTokenPos][1]=$value;
            return true;
        }
        return false;
    }
}
?>
